<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="个人昵称" prop="name">
      <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="登录邮箱" prop="email">
      <el-input type="email" v-model="ruleForm.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="输入密码" prop="pass">
      <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="pass2">
      <el-input type="password" v-model="ruleForm.pass2" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {reqLogin,reqRegister} from '../../api/index'
  import Vue from 'vue';
  import {Form,FormItem,Input,Button} from 'element-ui'
  Vue.use(Form)
  Vue.use(FormItem);
  Vue.use(Input);
  Vue.use(Button)

  export default {
    props:['loginHidden'],
    data() {
      let namePass = (rule,value,callback) =>{
        if (/^\S[\S\s]{0,9}$/.test(value)){

          callback()
        }
        callback(new Error('昵称长度1-10个字符'))
      }
      let emailPass = (rule,value,callback) =>{
        if (/^\s*$/.test(value)){

          callback(new Error('账号不能为空'))
        }
        callback();
      }
      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          pass2:'',
          name: '',
          email: ''

        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          pass2: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          name:[
            {validator:namePass,trigger:'blur'}
          ],
          email:[
            {validator:emailPass,trigger:'blur'}
          ]

        }
      };
    },
    methods: {
       submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let result = await reqRegister({email:this.ruleForm.email,password:this.ruleForm.pass,name:this.ruleForm.name},'post')
            console.log(result)
            localStorage.setItem('user', JSON.stringify(result))
            this.$store.commit('login',result.data)
            this.loginHidden()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>


<style scoped>
  .demo-ruleForm{
    width:500px;
  }
</style>
